home *** CD-ROM | disk | FTP | other *** search
/ AppleScript - The Beta Release / AppleScript - The Beta Release.iso / Documentation / Apple Event Registry / AE Suites Under Development / Word Services SDK 1.0 / Writeswell Jr. Source / TestBed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-25  |  14.6 KB  |  756 lines  |  [TEXT/KAHL]

  1. /* TestBed.c
  2.  * Word Services Testbed - main module for Writeswell Jr.
  3.  *
  4.  * ©1992 Working Software, Inc.
  5.  * This source code is copyrighted.  Permission is granted to use the Word Services
  6.  * portion of the Writeswell Jr. source code in your own programs, but you 
  7.  * may not distribute the Writeswell Jr. word-processor code as a 
  8.  * commercial product.  If you modify the code, please do not call it 
  9.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  10.  * program and don’t have to deal with a number of different versions with 
  11.  * who-knows-what going on in the code.
  12.  * 
  13.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  14. */
  15.  
  16. #include <EPPC.h>
  17. #include <AppleEvents.h>
  18. #include <AEObjects.h>
  19. #include <Printing.h>
  20. #include "AERegistry.h"
  21. #include "WordServices.h"
  22. #include "TestBed.h"
  23. #include "TBConstants.h"
  24. #include "MyFiles.h"
  25. #include "GenHandlers.h"
  26. #include "AppEvents.h"
  27. #include "AEObj.h"
  28. #include "InitMenu.h"
  29. #include "Gripe.h"
  30. #include "Scroll.h"
  31. #include "Prefs.h"
  32. #include "DoChecking.h"
  33. #include "ServiceMgr.h"
  34. #include "ObWind.h"
  35. #include "ObText.h"
  36. #include "ObNull.h"
  37. #include "FontMenu.h"
  38. #include "Options.h"
  39. #include "ServiceDialog.h"
  40. #include "PrintWWJr.h"
  41. #include "UnloadStuff.h"
  42. #include "InitWWJr.h"
  43. #include "MyGestalt.h"
  44. #include "PageSetup.h"
  45.  
  46. #define GLOBALS_HERE
  47. #include "TBGlobals.h"
  48. #undef GLOBALS_HERE
  49.  
  50. Boolean DoMouseDown( EventRecord *eventPtr );
  51. Boolean DoMenuCommand( long menuSpot );
  52. Boolean DoAppleMenu( short theItem );
  53. Boolean DoFileMenu( short theItem );
  54. Boolean DoEditMenu( short theItem );
  55. Boolean DoServMenu( short theItem );
  56. Boolean DoKeyDown( long message, short modifiers );
  57. Boolean DoActivateEvt( short modifiers );
  58. Boolean DoUpdateEvt( long message );
  59. void DoAboutMe( void );
  60. Boolean DoOSEvt( EventRecord *eventPtr );
  61. void GrowDocWindow( WindowPtr theWindow, Point where );
  62. void SetTESize( WindowPtr theWindow );
  63. void MyDrawGrowIcon( WindowPtr theWindow );
  64. void DoCloseWindow( void );
  65. Boolean AskSave( void );
  66. void DoQuit( void );
  67. void DoZoom( WindowPtr theWindow, short deskPart );
  68. void ResetWindowParts( WindowPtr theWindow );
  69.  
  70. void main( void )
  71. {
  72.     short foo;
  73.     OSErr    err;
  74.  
  75.     MaxApplZone();
  76.     InitGraf(&thePort);
  77.     InitFonts();
  78.     /* FlushEvents(everyEvent, 0); */
  79.     InitWindows();
  80.     TEInit();
  81.     InitDialogs(0L);
  82.     
  83.     gDocDirty = false;
  84.  
  85.     gAppFileRefNum = CurResFile();
  86.  
  87.     gQuitRequested = false;
  88.     gPrintRequested = false;
  89.  
  90.     if ( !OpenPrefFile() ){
  91.         Gripe( "\pCannot open Writeswell, Jr. Preferences" );
  92.         ExitToShell();
  93.     }
  94.  
  95.     InitCursor();
  96.  
  97.     if ( System7Installed() ){
  98.         err = InitForSystem7();
  99.     }else{
  100.         err = InitForSystem6();
  101.     }
  102.  
  103.     if ( err )
  104.         ExitToShell();
  105.  
  106.     while ( EventLoop() )
  107.         ;
  108. }
  109.  
  110. Boolean EventLoop( void )
  111. {
  112.     EventRecord theEvent;
  113.     
  114.     UnloadStuff();
  115.  
  116.     if ( gPrintRequested ){
  117.         gPrintRequested = false;
  118.         DoPrint();
  119.     }
  120.  
  121.     if ( gQuitRequested ){
  122.         DoQuit();
  123.         return false;            /* We should never reach this statement */
  124.     }
  125.  
  126.     WaitNextEvent( everyEvent, &theEvent, 30, (RgnHandle)NULL );
  127.     
  128.  
  129.     if ( gDocWindow )
  130.         TEIdle( (TEHandle)GetWRefCon( gDocWindow ) );
  131.     
  132.     switch( theEvent.what ){
  133.         case nullEvent:
  134.             DoNullEvent( &theEvent );
  135.             break;
  136.         case mouseDown:
  137.             return DoMouseDown( &theEvent );
  138.             break;
  139.         case mouseUp:
  140.             DoMouseUp();
  141.             break;
  142.         case autoKey:
  143.         case keyDown:
  144.             return DoKeyDown( theEvent.message, theEvent.modifiers );
  145.             break;
  146.         case updateEvt:
  147.             DoUpdateEvt( theEvent.message );
  148.             break;
  149.         case diskEvt:
  150.             DoDiskEvt();
  151.             break;
  152.         case activateEvt:
  153.             DoActivateEvt( theEvent.modifiers );
  154.             break;
  155.         case osEvt:
  156.             DoOSEvt( &theEvent );
  157.             break;
  158.         case kHighLevelEvent:
  159.             DoHighLevelEvent( &theEvent );
  160.             break;
  161.         default:
  162.             /*Gripe("\pGot unknown event type" );*/
  163.             break;    
  164.     }
  165.  
  166.     return true;
  167. }
  168.  
  169. Boolean DoNullEvent( EventRecord *theEventPtr )
  170. {
  171.     /* One could execute some sort of background task here */
  172.  
  173.     return true;
  174. }
  175.  
  176. Boolean DoMouseDown( EventRecord *eventPtr )
  177. {
  178.     WindowPtr    theWindow;
  179.     Boolean        result = true;
  180.     Rect        dragRect;
  181.     TEHandle    textH;
  182.     short        partCode;
  183.     ControlHandle    ctlHdl;
  184.     short        curFile;
  185.     short        deskPart;
  186.     
  187.     deskPart = FindWindow( eventPtr->where, &theWindow );
  188.     switch( deskPart ){
  189.         case inDesk:
  190.             break;
  191.         case inMenuBar:
  192.             FixMenuMarks();
  193.             curFile = CurResFile();
  194.             UseResFile( gPrefFileRefNum );        /* Make SICN resource accessible */
  195.             result = DoMenuCommand( MenuSelect( eventPtr->where ) );
  196.             UseResFile( curFile );
  197.             FixMenuMarks();
  198.             break;
  199.         case inSysWindow:
  200.             SystemClick( eventPtr, theWindow );
  201.             break;
  202.         case inContent:
  203.             if ( theWindow != FrontWindow() ){
  204.                 SelectWindow( theWindow );
  205.             } else {
  206.                 GlobalToLocal( &(eventPtr->where) );
  207.                 partCode = FindControl( eventPtr->where, theWindow, &ctlHdl );
  208.                 if ( partCode == 0 ){
  209.                     textH = (TEHandle)GetWRefCon( theWindow );
  210.                     
  211.                     gScrollWindow = theWindow;        /* For use by TrackContentClick */
  212.  
  213.                     TEClick( eventPtr->where, eventPtr->modifiers & shiftKey, textH );
  214.                 }else{
  215.                     DoControl( theWindow, ctlHdl, partCode, eventPtr->where );
  216.                 }
  217.             }
  218.             break;
  219.         case inDrag:
  220.             DragWindow( theWindow, eventPtr->where, &screenBits.bounds );    /* NOT correct but works */
  221.             break;
  222.         case inGrow:
  223.             GrowDocWindow( theWindow, eventPtr->where );
  224.             break;
  225.         case inGoAway:
  226.             if ( TrackGoAway( theWindow, eventPtr->where ))
  227.                 DoCloseWindow();
  228.             break;
  229.         case inZoomIn:
  230.         case inZoomOut:
  231.             if ( TrackBox( theWindow, eventPtr->where, deskPart ) )
  232.                 DoZoom( theWindow, deskPart );
  233.             break;
  234.     }
  235.     return result;
  236. }
  237.  
  238. void DoCloseWindow( void )
  239. {
  240.     Boolean saveIt;
  241.     
  242.     if ( gDocDirty ){
  243.         saveIt = AskSave();
  244.         if ( saveIt )
  245.             DoSave();
  246.     }
  247.  
  248.     TEDispose( (TEHandle)GetWRefCon( gDocWindow) );
  249.     DisposeWindow( gDocWindow );        
  250.     gDocWindow = (WindowPtr)NULL;
  251.     
  252.     if ( gDocExists ){
  253.         /* If the file exists on disk, close it */
  254.         FSClose( gRefNum );
  255.         if ( gResRefNum != -1 )
  256.             CloseResFile( gResRefNum );
  257.     }
  258.  
  259.     FixMenuMarks();            /* Disable menu items to reflect absent window */
  260.  
  261.     return;
  262. }
  263.  
  264. Boolean AskSave( void )
  265. {
  266.     short    item;
  267.     
  268.     item = Alert( rAskSaveID, (ProcPtr)NULL );
  269.  
  270.     if ( item == kASSave )
  271.         return true;
  272.     
  273.     return false;
  274. }
  275.  
  276. Boolean DoMenuCommand( long menuSpot )
  277. {
  278.     short theMenu;
  279.     short theItem;
  280.     Boolean result;
  281.         
  282.     theMenu = HiWord( menuSpot );
  283.     
  284.     theItem = LoWord( menuSpot );
  285.     
  286.     switch ( theMenu ){
  287.         case 0:
  288.             result = true;
  289.             break;
  290.         case kAppleMenuID:
  291.             result = DoAppleMenu( theItem );
  292.             break;
  293.         case kFileMenuID:
  294.             result = DoFileMenu( theItem );
  295.             break;
  296.         case kEditMenuID:
  297.             result = DoEditMenu( theItem );
  298.             break;
  299.         case kFontMenuID:
  300.             result = DoFontMenu( theItem );
  301.             break;
  302.         case kStyleMenuID:
  303.             result = DoStyleMenu( theItem );
  304.             break;
  305.         case kServMenuID:
  306.             result = DoServMenu( theItem );
  307.             break;
  308.     }
  309.     
  310.     HiliteMenu( 0 );
  311.     return result;
  312. }
  313.  
  314. Boolean DoAppleMenu( short theItem )
  315. {
  316.     Handle appleHandle;
  317.     Str255 itemName;
  318.     
  319.     switch ( theItem ){
  320.         case kAMAboutMe:
  321.             DoAboutMe();
  322.             break;
  323.         case kAMDash:
  324.             break;
  325.         default:
  326.             appleHandle = GetResource( 'MENU', kAppleMenuID );
  327.             if ( !appleHandle ){
  328.                 Gripe( "\pCannot get handle to apple menu" );
  329.                 return false;
  330.             }
  331.             
  332.             GetItem( appleHandle, theItem, itemName );
  333.             OpenDeskAcc( itemName );
  334.             if ( gDocWindow ){
  335.                 SetPort( gDocWindow );
  336.             }
  337.             break;
  338.     }
  339.     return true;
  340. }
  341.  
  342. Boolean DoFileMenu( short theItem )
  343. {
  344.     switch ( theItem ){
  345.         case kFMNew:
  346.             if ( gDocWindow )
  347.                 return true;
  348.             
  349.             return MakeNewWindow() == noErr;
  350.             break;
  351.         case kFMOpen:
  352.             DoOpenDialog();
  353.             break;
  354.         case kFMClose:
  355.             DoCloseWindow();
  356.             break;
  357.         case kFMSave:
  358.             DoSave();
  359.             break;
  360.         case kFMSaveAs:
  361.             DoSaveDialog();
  362.             break;
  363.         case kFMPageSetup:
  364.             DoPageSetup();
  365.             break;
  366.         case kFMPrint:
  367.             DoPrint();
  368.             break;
  369.         case kFMQuit:
  370.             DoQuit();
  371.             break;
  372.     }
  373.     
  374.     return true;
  375. }
  376.  
  377. Boolean DoEditMenu( short theItem )
  378. {
  379.     TEHandle textH;
  380.  
  381.     if ( !SystemEdit( theItem - 1 ) ){
  382.         if ( gDocWindow ){
  383.             textH = (TEHandle)GetWRefCon( gDocWindow );
  384.             
  385.             switch( theItem ){
  386.                 case kEMCut:
  387.                     gDocDirty = true;
  388.                     TECut( textH );
  389.                     break;
  390.                 case kEMCopy:
  391.                     TECopy( textH );
  392.                     break;
  393.                 case kEMPaste:
  394.                     gDocDirty = true;
  395.                     TEStylPaste( textH );
  396.                     break;
  397.                 case kEMClear:
  398.                     gDocDirty = true;
  399.                     TEDelete( textH );
  400.                     break;
  401.                 case kEMOptions:
  402.                     OptionsDialog();
  403.                     break;
  404.             }
  405.             
  406.             SetVertScroll( gDocWindow, gVertScroll );
  407.         }else{
  408.             /* Handle the operations that we can do while the window is closed */
  409.             switch( theItem ){
  410.                 case kEMOptions:
  411.                     OptionsDialog();
  412.                     break;
  413.             }
  414.         
  415.         }
  416.     }
  417.     return true;
  418. }
  419.  
  420. Boolean DoServMenu( short theItem )
  421. {
  422.     OSErr err;
  423.  
  424.     switch( theItem ){
  425. #ifdef NEVER                /* This will return in a future version */
  426.         case kSMCheckSel:
  427.             ToggleSelectCheck();
  428.             break;
  429. #endif
  430.         case kSMNewBatch:
  431.             err = GetNewBatchService();
  432.             if ( err )
  433.                 Gripe( "\pGetNewBatchService failed" );
  434.             break;
  435. #ifdef NEVER                /* This will return in a future version */
  436.         case kSMNewInteractive:
  437.             if ( !gDocWindow )
  438.                 return true;
  439.             break;
  440. #endif
  441.         case kSMRemoveService:
  442.             RemoveServices();
  443.             break;
  444. #ifdef NEVER                /* This will return in a future version */
  445.         case kSMCheckWord:
  446.             if ( !gDocWindow )
  447.                 return true;
  448.             break;
  449. #endif
  450.         case kSMDash:
  451.             break;
  452.         default:
  453.             /* Here is where we actually do some spellchecking! */
  454.             if ( !gDocWindow )
  455.                 return true;
  456.             
  457.             DoSpellCheck( theItem - kSMDash );
  458.             
  459.             break;
  460.     }
  461.     return true;
  462. }
  463.     
  464. Boolean DoMouseUp( void )
  465. {
  466.     return true;
  467. }
  468.  
  469. Boolean DoKeyDown( long message, short modifiers )
  470. {
  471.     char        theChar;
  472.     Boolean        result;
  473.     TEHandle    textH;
  474.     short        numLines;
  475.     
  476.     theChar = message & charCodeMask;
  477.     
  478.     if ( modifiers & cmdKey ){
  479.         FixMenuMarks();        /* We need to have the dis/enabling up to date */
  480.         result = DoMenuCommand( MenuKey( theChar ) );
  481.     }else {
  482.         if ( gDocWindow && ( gDocWindow == FrontWindow() )){
  483.             gDocDirty = true;
  484.             textH = (TEHandle)GetWRefCon( gDocWindow );
  485.             
  486.             numLines = (*textH)->nLines;
  487.             TEKey( theChar, textH );
  488.             if ( numLines != (*textH)->nLines ){
  489.                 /* We may need to reset the scroll bar if we changed the number of lines */
  490.                 ShowSelection( textH );
  491.                 SetVertScroll(    gDocWindow, gVertScroll );
  492.             }
  493.         }
  494.         result = true;
  495.     }
  496.         
  497.     return result;
  498. }
  499.  
  500. Boolean DoUpdateEvt( long message )
  501. {
  502.     GrafPtr    curPort;
  503.     
  504.     BeginUpdate( (WindowPtr)message );
  505.     
  506.     if ( (WindowPtr)message == gDocWindow ){
  507.         GetPort( &curPort );
  508.         SetPort( gDocWindow );
  509.         EraseRect( &( thePort->portRect ) );
  510.         TEUpdate( &( thePort->portRect ), (TEHandle)GetWRefCon( (WindowPtr)message ) );
  511.         MyDrawGrowIcon( (WindowPtr)message );
  512.         DrawControls( gDocWindow );
  513.         SetPort( curPort );
  514.     }
  515.  
  516.     EndUpdate( (WindowPtr)message );
  517.     return true;
  518. }
  519.  
  520. void MyDrawGrowIcon( WindowPtr theWindow )
  521. {
  522.     RgnHandle    clipRgn;
  523.     Rect        newClip;
  524.     
  525.     /* This will fudge the DrawGrowIcon routine so we only draw the outline for the
  526.      * vertical scroll bar.  It's easier than writing a custom WDEF.
  527.      * We have to set a new clip region because the update region will mask off some
  528.      * of what we might want to draw.
  529.      *
  530.      * thePort must be the window that is getting the icon drawn in it.
  531.      */
  532.  
  533.     clipRgn = NewRgn();
  534.     if ( !clipRgn )
  535.         return;
  536.     
  537.     GetClip( clipRgn );
  538.     
  539.     newClip.top = theWindow->portRect.top;
  540.     newClip.right = theWindow->portRect.right;
  541.     newClip.bottom = theWindow->portRect.bottom;
  542.     newClip.left = newClip.right - 15;
  543.     
  544.     ClipRect( &newClip );
  545.     
  546.     DrawGrowIcon( theWindow );
  547.  
  548.     SetClip( clipRgn );
  549.     
  550.     return;
  551. }
  552.  
  553. Boolean DoDiskEvt( void )
  554. {
  555.     return true;
  556. }
  557.  
  558. Boolean DoActivateEvt( short modifiers )
  559. {
  560.  
  561.     if ( gDocWindow ){
  562.  
  563.         if ( modifiers & activeFlag ){
  564.             TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
  565.         } else {
  566.             TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
  567.         }
  568.     }
  569.     return true;
  570. }
  571.  
  572. void GrowDocWindow( WindowPtr theWindow, Point where )
  573. {
  574.     long    newSize;
  575.     short    newWidth;
  576.     short    newHeight;
  577.     GrafPtr    curPort;
  578.     Rect    sizeRect;
  579.     
  580.     SetRect( &sizeRect, 80, 80, 32767, 32767 );
  581.  
  582.     newSize = GrowWindow( theWindow, where, &sizeRect );
  583.     if ( newSize == 0L )
  584.         return;
  585.  
  586.     newWidth = (short) newSize;
  587.     newHeight = (short) ( newSize >> 16 );
  588.     
  589.     GetPort( &curPort );
  590.     SetPort( theWindow );
  591.  
  592.     SizeWindow( theWindow, newWidth, newHeight, true );
  593.  
  594.     ResetWindowParts( theWindow );
  595.  
  596.     SetPort( curPort );
  597.  
  598.     return;
  599. }
  600.  
  601. void ResetWindowParts( WindowPtr theWindow )
  602. {
  603.     GrafPtr    curPort;
  604.  
  605.     /* Resize all of the components of the document window after the window itself
  606.      * has been resized.
  607.      */
  608.  
  609.     GetPort( &curPort );
  610.     SetPort( theWindow );
  611.  
  612.     SetTESize( theWindow );
  613.  
  614.     /* We have to redraw the whole window, since the grow icon, scroll bars, and
  615.      * text rectange have all changed
  616.      */
  617.     
  618.     InvalRect( &( theWindow->portRect ) );
  619.     
  620.     SizeVertScroll();
  621.     SetVertScroll( theWindow, gVertScroll );
  622.     
  623.     SetPort( curPort );
  624.  
  625.     return;
  626. }
  627.  
  628. Boolean DoOSEvt( EventRecord *eventPtr )
  629. {
  630.     Boolean inBackground;
  631.     
  632.     switch ((eventPtr->message >> 24) & 0x0FF) {     /* high byte of message */
  633.         case suspendResumeMessage:              /* suspend/resume is also an activate/deactivate */
  634.             if ( gDocWindow ){
  635.  
  636.                 inBackground = (eventPtr->message & resumeFlag) == 0;
  637.                 if (inBackground) {
  638. #ifdef NEVER        /* Don't use for styled textedit */
  639.                     ZeroScrap();
  640.                     TEToScrap();
  641. #endif
  642.                     if ( gDocWindow ){
  643.                         TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
  644.                     }
  645.                 } else {
  646. #ifdef NEVER        /* Don't use for styled textedit */
  647.                     TEFromScrap();
  648. #endif
  649.                     if ( gDocWindow ){
  650.                         TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
  651.                     }
  652.                 }
  653.             }
  654.             
  655.         default:
  656.             break;
  657.     }
  658.     return true;
  659. }
  660.  
  661. Boolean DoHighLevelEvent( EventRecord *theEventPtr )
  662. {
  663.     OSErr err;
  664.     
  665.     err = AEProcessAppleEvent( theEventPtr );
  666.  
  667.     if ( err ){
  668.         return true;            /* An error result is OK... */
  669.     }
  670.  
  671.     return true;
  672. }
  673.  
  674. void DoAboutMe( void )
  675. {
  676.     DialogPtr dlg;
  677.     short item;
  678.     
  679.     dlg = GetNewDialog( kAboutMeID, (Ptr)NULL, (WindowPtr) -1 );
  680.  
  681.     if ( !dlg )
  682.         return;
  683.     
  684.     do {
  685.         ModalDialog( (ProcPtr)NULL, &item );
  686.     } while ( item != 1 );
  687.     
  688.     DisposDialog( dlg );
  689.     
  690.     return;
  691. }
  692.  
  693. void SetTESize( WindowPtr theWindow )
  694. {
  695.     Rect    txRect;
  696.     TEHandle    hTE;
  697.     short    rectOffset;
  698.     
  699.     GetTERect( &( theWindow->portRect ), &txRect );
  700.     
  701.     hTE = (TEHandle)GetWRefCon( theWindow );
  702.     
  703.     rectOffset = (*hTE)->destRect.top - (*hTE)->viewRect.top;
  704.  
  705.     (*hTE)->viewRect = txRect;
  706.  
  707.     txRect.top += rectOffset;
  708.     txRect.bottom += rectOffset;
  709.  
  710.     (*hTE)->destRect = txRect;
  711.  
  712.     TECalText( hTE );
  713.  
  714.     return;
  715. }
  716.  
  717. void GetTERect( Rect *portRectPtr, Rect *txRectPtr )
  718. {
  719.     /* Return the rect for the textedit field that will fit in the
  720.      * given portRect, allowing for the scroll bar, and a little bit
  721.      * of margin around all the edges.
  722.      */
  723.  
  724.     *txRectPtr = *portRectPtr;
  725.     InsetRect( txRectPtr, 4, 0 );
  726.     
  727.     txRectPtr->right -= 16;
  728.  
  729.     return;
  730. }
  731.  
  732. void DoQuit( void )
  733. {
  734.     if ( gDocWindow )
  735.         DoCloseWindow();
  736.     ExitToShell();
  737.     
  738.     return;
  739. }
  740.  
  741. void DoZoom( WindowPtr theWindow, short deskPart )
  742. {
  743.     Boolean isFront;
  744.     
  745.     if ( FrontWindow() == theWindow )
  746.         isFront = true;
  747.     else
  748.         isFront = false;
  749.  
  750.     ZoomWindow( theWindow, deskPart, isFront );
  751.     
  752.     ResetWindowParts( theWindow );
  753.  
  754.     return;
  755. }
  756.